/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.crunchutils;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.UnhandledException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A set of utility methods to operate on strings
 * @author James Selvakumar
 */
public class CrunchStringUtils
{
    private static final Logger logger = LoggerFactory.getLogger(CrunchStringUtils.class);

    /**
     * Generate a md5 checksum for the text
     * @param text The input text
     * @return
     */
    public static String generateMd5(String text)
    {
        try {
            MessageDigest digest = MessageDigest.getInstance("MD5");
            digest.update(text.getBytes(), 0, text.length());
            return new BigInteger(1, digest.digest()).toString(16);
        } catch (NoSuchAlgorithmException e) {
            throw new UnhandledException("MD5 ist not available: ", e);
        }
    }

    /**
     * Formats the given number using the decimal format "###,###"
     * @param number The number to be formatted
     * @return
     */
    public static String formatNumber(Object number)
    {
        DecimalFormat formatter = new DecimalFormat("###,###");
        return formatter.format(number);
    }

    /**
     * Splits the given string by size
     * @param stringToSplit The input string
     * @param size The size of the slices
     * @return a list containing the string slices
     */
    public static List<String> splitBySize(String stringToSplit, int size)
    {
        List<String> list = new ArrayList<String>();
        if (stringToSplit.length() > size) {
            //if the stringToSplit is not completely divisible by size, then add "1" to the quotient
            int splitCount = (stringToSplit.length() % size) == 0 ? stringToSplit.length() / size : (stringToSplit.length() / size) + 1;
            logger.debug("Length of the string: " + stringToSplit.length()
                    + ". The string can be split into " + splitCount + " parts.");
            int startIndex = 0;
            int endIndex = size;

            for (int i = 0; i < splitCount; i++) {
                logger.debug("Start index: " + startIndex + ", End index: " + endIndex);

                if (endIndex <= stringToSplit.length()) {
                    String splittedText = stringToSplit.substring(startIndex, endIndex);
                    logger.debug("Splitted text: " + splittedText);
                    list.add(splittedText);
                } else {
                    String splittedText = stringToSplit.substring(startIndex, stringToSplit.length());
                    logger.debug("Splitted text-> " + splittedText);
                    list.add(splittedText);
                }
                startIndex = endIndex;
                endIndex = endIndex + size;
            }
        } else {
            list.add(stringToSplit);
        }
        return list;
    }
}
